﻿using System;
using System.IO;
using Hongsh.Pay.Client.Proxy.Http;
using MapleLeaf.Pay;
using MapleLeaf.Pay.Pay;
using MapleLeaf.Pay.Trade;

namespace Hongsh.Pay.Client.Proxy.Handlers {
    // Token: 0x02000045 RID: 69
    public static class BY0PayProxy {
        // Token: 0x06000212 RID: 530 RVA: 0x00003619 File Offset: 0x00001819
        public static void AddBY0PayProxyHandler(this HttpServer svr) {
            svr.RouterList.Add("POST/api/md5", new HttpContextActionDelegate(Process));
        }

        // Token: 0x06000213 RID: 531 RVA: 0x0000D3D0 File Offset: 0x0000B5D0
        private static void Process(HttpContext ctx) {
            string json = new StreamReader(ctx.Request).ReadToEnd();
            BY0PayRequest by0PayRequest = json.JsonDecode<BY0PayRequest>();
            if (by0PayRequest.servicecode == "BsCPay") {
                GClass2 gclass = new GClass2 {
                    status = "0"
                };
                try {
                    HongshPayTrade hongshPayTrade = ctx.Execute(new TradeAuthPaymentRequest {
                        Amount = (int)Math.Ceiling(decimal.Parse(by0PayRequest.data.amount) * 100m),
                        Authcode = by0PayRequest.data.authCode,
                        ClientTradeId = by0PayRequest.data.orderNo
                    });
                    gclass.orderNo = by0PayRequest.data.orderNo;
                    gclass.tradeNo = hongshPayTrade.TradeId;
                    DateTime t = DateTime.Now.AddSeconds(50.0);
                    while (hongshPayTrade.State == TradeState.未知 && DateTime.Now <= t) {
                        hongshPayTrade = ctx.Execute(new TradePaymentQueryRequest {
                            TradeId = gclass.tradeNo
                        });
                    }
                    if (hongshPayTrade.State != TradeState.成功) {
                        throw new Exception(hongshPayTrade.StateDescript);
                    }
                    gclass.payType = (hongshPayTrade.Payway == TradePayway.微信) ? "Wx" : "Ali";
                    gclass.tradeTime = hongshPayTrade.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss");
                    gclass.status = "1";
                    gclass.statusDes = "支付成功";
                    gclass.amount = by0PayRequest.data.amount;
                }
                catch (Exception ex) {
                    gclass.statusDes = ex.Message;
                }
                ctx.Result = new HttpJsonResult(new BY0PayResult {
                    flag = "1",
                    data = gclass,
                    sign = Guid.NewGuid().ToString("N")
                });
            }
            else {
                ctx.Result = HttpResult.NoFound;
            }
        }

        // Token: 0x02000046 RID: 70
        public class GClass1 {
            // Token: 0x170000E2 RID: 226
            // (get) Token: 0x06000214 RID: 532 RVA: 0x00003637 File Offset: 0x00001837
            // (set) Token: 0x06000215 RID: 533 RVA: 0x0000363F File Offset: 0x0000183F
            public string amount { get; set; }

            // Token: 0x170000E3 RID: 227
            // (get) Token: 0x06000216 RID: 534 RVA: 0x00003648 File Offset: 0x00001848
            // (set) Token: 0x06000217 RID: 535 RVA: 0x00003650 File Offset: 0x00001850
            public string authCode { get; set; }

            // Token: 0x170000E4 RID: 228
            // (get) Token: 0x06000218 RID: 536 RVA: 0x00003659 File Offset: 0x00001859
            // (set) Token: 0x06000219 RID: 537 RVA: 0x00003661 File Offset: 0x00001861
            public string operatorNo { get; set; }

            // Token: 0x170000E5 RID: 229
            // (get) Token: 0x0600021A RID: 538 RVA: 0x0000366A File Offset: 0x0000186A
            // (set) Token: 0x0600021B RID: 539 RVA: 0x00003672 File Offset: 0x00001872
            public string orderNo { get; set; }

            // Token: 0x170000E6 RID: 230
            // (get) Token: 0x0600021C RID: 540 RVA: 0x0000367B File Offset: 0x0000187B
            // (set) Token: 0x0600021D RID: 541 RVA: 0x00003683 File Offset: 0x00001883
            public string remark { get; set; }

            // Token: 0x170000E7 RID: 231
            // (get) Token: 0x0600021E RID: 542 RVA: 0x0000368C File Offset: 0x0000188C
            // (set) Token: 0x0600021F RID: 543 RVA: 0x00003694 File Offset: 0x00001894
            public string shopNo { get; set; }

            // Token: 0x170000E8 RID: 232
            // (get) Token: 0x06000220 RID: 544 RVA: 0x0000369D File Offset: 0x0000189D
            // (set) Token: 0x06000221 RID: 545 RVA: 0x000036A5 File Offset: 0x000018A5
            public string payType { get; set; }

            // Token: 0x06000222 RID: 546 RVA: 0x0000286D File Offset: 0x00000A6D
            public GClass1() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }

        // Token: 0x02000047 RID: 71
        public class BY0PayRequest {
            // Token: 0x170000E9 RID: 233
            // (get) Token: 0x06000223 RID: 547 RVA: 0x000036AE File Offset: 0x000018AE
            // (set) Token: 0x06000224 RID: 548 RVA: 0x000036B6 File Offset: 0x000018B6
            public string apiversion { get; set; }

            // Token: 0x170000EA RID: 234
            // (get) Token: 0x06000225 RID: 549 RVA: 0x000036BF File Offset: 0x000018BF
            // (set) Token: 0x06000226 RID: 550 RVA: 0x000036C7 File Offset: 0x000018C7
            public string source { get; set; }

            // Token: 0x170000EB RID: 235
            // (get) Token: 0x06000227 RID: 551 RVA: 0x000036D0 File Offset: 0x000018D0
            // (set) Token: 0x06000228 RID: 552 RVA: 0x000036D8 File Offset: 0x000018D8
            public string servicecode { get; set; }

            // Token: 0x170000EC RID: 236
            // (get) Token: 0x06000229 RID: 553 RVA: 0x000036E1 File Offset: 0x000018E1
            // (set) Token: 0x0600022A RID: 554 RVA: 0x000036E9 File Offset: 0x000018E9
            public string sign { get; set; }

            // Token: 0x170000ED RID: 237
            // (get) Token: 0x0600022B RID: 555 RVA: 0x000036F2 File Offset: 0x000018F2
            // (set) Token: 0x0600022C RID: 556 RVA: 0x000036FA File Offset: 0x000018FA
            public GClass1 data { get; set; }

            // Token: 0x0600022D RID: 557 RVA: 0x0000286D File Offset: 0x00000A6D
            public BY0PayRequest() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }

        // Token: 0x02000048 RID: 72
        public class GClass2 {
            // Token: 0x170000EE RID: 238
            // (get) Token: 0x0600022E RID: 558 RVA: 0x00003703 File Offset: 0x00001903
            // (set) Token: 0x0600022F RID: 559 RVA: 0x0000370B File Offset: 0x0000190B
            public string status { get; set; }

            // Token: 0x170000EF RID: 239
            // (get) Token: 0x06000230 RID: 560 RVA: 0x00003714 File Offset: 0x00001914
            // (set) Token: 0x06000231 RID: 561 RVA: 0x0000371C File Offset: 0x0000191C
            public string statusDes { get; set; }

            // Token: 0x170000F0 RID: 240
            // (get) Token: 0x06000232 RID: 562 RVA: 0x00003725 File Offset: 0x00001925
            // (set) Token: 0x06000233 RID: 563 RVA: 0x0000372D File Offset: 0x0000192D
            public string payType { get; set; }

            // Token: 0x170000F1 RID: 241
            // (get) Token: 0x06000234 RID: 564 RVA: 0x00003736 File Offset: 0x00001936
            // (set) Token: 0x06000235 RID: 565 RVA: 0x0000373E File Offset: 0x0000193E
            public string tradeTime { get; set; }

            // Token: 0x170000F2 RID: 242
            // (get) Token: 0x06000236 RID: 566 RVA: 0x00003747 File Offset: 0x00001947
            // (set) Token: 0x06000237 RID: 567 RVA: 0x0000374F File Offset: 0x0000194F
            public string tradeNo { get; set; }

            // Token: 0x170000F3 RID: 243
            // (get) Token: 0x06000238 RID: 568 RVA: 0x00003758 File Offset: 0x00001958
            // (set) Token: 0x06000239 RID: 569 RVA: 0x00003760 File Offset: 0x00001960
            public string amount { get; set; }

            // Token: 0x170000F4 RID: 244
            // (get) Token: 0x0600023A RID: 570 RVA: 0x00003769 File Offset: 0x00001969
            // (set) Token: 0x0600023B RID: 571 RVA: 0x00003771 File Offset: 0x00001971
            public string orderNo { get; set; }

            // Token: 0x0600023C RID: 572 RVA: 0x0000286D File Offset: 0x00000A6D
            public GClass2() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }

        // Token: 0x02000049 RID: 73
        public class BY0PayResult {
            // Token: 0x170000F5 RID: 245
            // (get) Token: 0x0600023D RID: 573 RVA: 0x0000377A File Offset: 0x0000197A
            // (set) Token: 0x0600023E RID: 574 RVA: 0x00003782 File Offset: 0x00001982
            public string flag { get; set; }

            // Token: 0x170000F6 RID: 246
            // (get) Token: 0x0600023F RID: 575 RVA: 0x0000378B File Offset: 0x0000198B
            // (set) Token: 0x06000240 RID: 576 RVA: 0x00003793 File Offset: 0x00001993
            public string sign { get; set; }

            // Token: 0x170000F7 RID: 247
            // (get) Token: 0x06000241 RID: 577 RVA: 0x0000379C File Offset: 0x0000199C
            // (set) Token: 0x06000242 RID: 578 RVA: 0x000037A4 File Offset: 0x000019A4
            public GClass2 data { get; set; }

            // Token: 0x06000243 RID: 579 RVA: 0x0000286D File Offset: 0x00000A6D
            public BY0PayResult() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }
    }
}
